import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import postCssPxToRem from "postcss-pxtorem";
import dts from "vite-plugin-dts";
import legacy from "@vitejs/plugin-legacy";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  console.log('env',env)
  return {
    base: "./",
    plugins: [
      vue(),
      dts(),
      legacy({
        targets: ["defaults", "not IE 11"],
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",
      }),
    ],
    resolve: {
      // ↓路径别名
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      proxy: {
        '/dev-api': {
          target: 'http://www.baidu.com/',
          changeOrigin: true,
          rewrite: (path) => path.replace("^/dev-api", ""),
        },
      },
      port: 8000,
    },

    css: {
      postcss: {
        plugins: [
          postCssPxToRem({
            rootValue: 192, // 按照自己的设计稿修改 1920/10
            unitPrecision: 5, // 保留到5位小数
            selectorBlackList: ["ignore"], // 忽略转换正则匹配项
            propList: ["*", "!border"],
            replace: true,
            mediaQuery: false, // 媒体查询( @media screen 之类的)中不生效
            minPixelValue: 1, //可以选择px小于1的不会被转换
          }),
        ],
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
    },
  };
});
